home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / elv18src.zip / sysos2.c < prev    next >
C/C++ Source or Header  |  1994-01-10  |  5KB  |  267 lines

  1. /* sysos2.c  -- OS/2 version of system.c */
  2.  
  3. /* Authors:
  4.  *    Kai Uwe Rommel, rommel@ars.muc.de
  5.  *    Greg R Roelofs, newt@uchicago.edu
  6.  */
  7.  
  8. #include "config.h"
  9. #include "vi.h"
  10.  
  11. #include <stdio.h>
  12. #include <process.h>
  13.  
  14. #define    INCL_DOSPROCESS        /* doesn't exist in emx 0.8f...? */
  15. #define INCL_VIO
  16. #include <os2.h>
  17.  
  18. void raw_set_stdio(rawstate)
  19.     int rawstate;
  20. {
  21.       static int buffered = 0;
  22.  
  23.     if (!buffered)
  24.     {
  25.       buffered = 1;
  26.       setvbuf(stdout, NULL, _IOFBF, BUFSIZ);
  27.     }
  28.  
  29.     if (rawstate)
  30.       setmode(fileno(stdout), O_BINARY);
  31.     else
  32.       setmode(fileno(stdout), O_TEXT);
  33. }
  34.  
  35. int system(cmd)
  36.     const    char    *cmd;    /* a command to run */
  37. {
  38.     int    status;    /* exit status of the command */
  39.  
  40.     if (cmd == o_shell)
  41.       status = spawnl(P_WAIT, o_shell, o_shell, 0);
  42.     else
  43.       status = spawnl(P_WAIT, o_shell, o_shell, "/c", cmd, 0);
  44.  
  45.     return status;
  46. }
  47.  
  48. static int pid[64];
  49.  
  50. int rpipe(cmd, in)
  51.     char    *cmd;    /* the filter command to use */
  52.     int    in;    /* the fd to use for stdin */
  53. {
  54.     int    r0w1[2];/* the pipe fd's */
  55.     int     old0, old1;
  56.  
  57.     if (pipe(r0w1) < 0)
  58.         return -1;
  59.  
  60.     /* save the parent's stdin, redirect the child's */
  61.     old0 = dup(0);
  62.     fcntl(old0, F_SETFD, 1);
  63.     dup2(in, 0);
  64.  
  65.     /* save the parent's stdout, redirect the child's */
  66.     old1 = dup(1);
  67.     fcntl(old1, F_SETFD, 1);
  68.     dup2(r0w1[1], 1);
  69.  
  70.     /* let the parent forget the writing end of the pipe */
  71.     close(r0w1[1]);
  72.     /* and don't let the child inherit the reading end of the pipe */
  73.     fcntl(r0w1[0], F_SETFD, 1);
  74.  
  75.     pid[r0w1[0]] = spawnl(P_NOWAIT, o_shell, o_shell, "/c", cmd, 0);
  76.  
  77.     /* restore parent's stdin */
  78.     dup2(old0, 0);
  79.     close(old0);
  80.  
  81.     /* restore parent's stdout */
  82.     dup2(old1, 1);
  83.     close(old1);
  84.  
  85.     /* return reading end of pipe, i.e. child's output */
  86.     return r0w1[0];
  87. }
  88.  
  89. int rpclose(fd)
  90.     int    fd;
  91. {
  92.       int    status;
  93.  
  94.     close(fd);
  95.     waitpid(pid[fd], &status, 0);
  96.  
  97.     return status;
  98. }
  99.  
  100. char *gethome(exe)
  101.     char    *exe;    /* full pathname of the ELVIS.EXE file */
  102. {
  103.     static char    *home;
  104.  
  105.     if (!home)
  106.     {
  107.       home = getenv("HOME");
  108.  
  109.       if (!home)
  110.       {
  111.         PTIB pptib;
  112.         PPIB pppib;
  113.         char *path;
  114.  
  115.         DosGetInfoBlocks(&pptib, &pppib);
  116.  
  117.         path = pppib -> pib_pchenv;
  118.  
  119.         while (*path)
  120.           path = strchr(path, 0) + 1;
  121.  
  122.         home = path + 1;
  123.       }
  124.     }
  125.  
  126.     return home;
  127. }
  128.  
  129. void v_sr()   /* scroll reverse:  scroll whole screen down one line */
  130. {
  131.     CHAR cell[2];
  132.     short len = sizeof(cell);
  133.  
  134.     refresh();   /* flush buffer before scrolling */
  135.     VioReadCellStr(cell, &len, 0, 0, 0);   /* read current screen attr */
  136.     cell[0] = ' ';
  137.     VioScrollDn(0, 0, -1, -1, 1, cell, 0);
  138. }
  139.  
  140. void audible_beep()   /* same pitch but shorter duration than ^G */
  141. {
  142.     DosBeep(800, 32);
  143. }
  144.  
  145. static did_main_init = FALSE;
  146. static ansi = FALSE;        /* assume no need to turn on ANSI */
  147. static quit_attr = 0x07;    /* assume light gray on black, by default */
  148.  
  149. /* determine size of screen and set of attributes to use, and store
  150.  * initial state */
  151. void v_vio_init()
  152. {
  153.     char *str;
  154.     CHAR cell[2];
  155.     /* USHORT val = 2; */
  156.     USHORT row, col, val=2;
  157.  
  158.     /* v_vio_init() is called from initscr(), which is called from main()
  159.      * before options and EXINIT are processed...uh oh.  So do our own
  160.      * quick and dirty pre-processing of EXINIT variable to see if vs or
  161.      * viomode is set there.
  162.      */
  163.     str = getenv(EXINIT);
  164.     if (str)
  165.     {
  166.         strlwr(str);
  167.         if (strstr(str, "set viomode") != NULL ||
  168.             strstr(str, "set vm") != NULL)
  169.         {
  170.             *o_viomode = TRUE;
  171.         }
  172.     }
  173.     if (!*o_viomode)    /* not allowed to use these functions */
  174.     {
  175.         return;
  176.     }
  177.  
  178.     VioGetCurPos(&row, &col, 0);
  179.     if (row > 0)
  180.     {
  181.         --row;    /* cursor is on fresh line after CR: check line above */
  182.     }
  183.  
  184.     /* get attribute of first character of command line and save for exit */
  185.         VioReadCellStr(cell, &val, row, 0, 0);
  186.     if (val > 0)
  187.     {
  188.         quit_attr = cell[1];
  189.     }
  190.  
  191.     VioGetAnsi(&val, 0);
  192.     if (val == ANSI_OFF)
  193.     {
  194.         VioSetAnsi(ANSI_ON, 0);
  195.         ansi = TRUE;
  196.     }
  197.  
  198.     did_main_init = TRUE;
  199. }
  200.  
  201. /* restore initial video state (more or less) */
  202. void v_vio_restore()
  203. {
  204.     if (!did_main_init)
  205.     {
  206.         /* didn't save anything, so don't try to restore it */
  207.         return;
  208.     }
  209.  
  210.     if (ansi)    /* ANSI was off and we turned it on; turn off again */
  211.     {
  212.         CHAR cell[3] = {' ', (CHAR)quit_attr, '\0'};
  213.         USHORT row, col, count;
  214.  
  215.         VioSetAnsi(ANSI_OFF, 0);
  216.         VioGetCurPos(&row, &col, 0);
  217.         count = COLS - col;
  218.         VioWrtNCell(cell, count, row, col, 0);    /* cursor stays put */
  219.     }
  220.     else
  221.     {
  222.         static char sgr2[8] = {'0', '4', '2', '6', '1', '5', '3', '7'};
  223.         char sgrbuf[24];
  224.  
  225.         /* build ANSI SGR string out of BIOS code of saved color via
  226.          * lookup table; honor foreground color (bits 0-2), background
  227.          * color (bits 4-6), and bold/normal attribute (bit 3), but
  228.          * forget about blinking attribute (bit 7):  too bad
  229.          */
  230.         sprintf(sgrbuf, "\033[%c;3%c;4%cm", (quit_attr & 8)? '1' : '0', 
  231.             sgr2[quit_attr & 0x07], sgr2[(quit_attr & 0x70) >> 4]);
  232.         qaddstr(sgrbuf);
  233.         refresh();
  234.     }
  235. }
  236.  
  237. #ifndef NO_CURSORSHAPE
  238.  
  239. static VIOCURSORINFO vioci;
  240. static did_cursor_init = FALSE;
  241.  
  242. /* cursor big: set begin scan to end scan - 4 */
  243. void v_cb()
  244. {
  245.     if (!did_cursor_init)
  246.     {
  247.         VioGetCurType(&vioci, 0);
  248.         did_cursor_init = TRUE;
  249.     }
  250.     vioci.yStart = vioci.cEnd - 4;    /* cEnd never changes */
  251.     VioSetCurType(&vioci, 0);
  252. }
  253.  
  254. /* cursor small:  set begin scan to end scan - 1 */
  255. void v_cs()
  256. {
  257.     if (!did_cursor_init)
  258.     {
  259.         VioGetCurType(&vioci, 0);
  260.         did_cursor_init = TRUE;
  261.     }
  262.     vioci.yStart = vioci.cEnd - 1;    /* cEnd never changes */
  263.     VioSetCurType(&vioci, 0);
  264. }
  265.  
  266. #endif /* !NO_CURSORSHAPE */
  267.